package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.service.SetmealGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.SetmealGroupMapper;
import com.itheima.pojo.SetmealGroup;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.ArrayList;
import java.util.Arrays;

@DubboService
public class SetmealGroupServiceImpl extends ServiceImpl<SetmealGroupMapper, SetmealGroup> implements SetmealGroupService {

    /**
     * 删除中间表根据setmeal的主键删除
     * @param id setmeal的ID
     */
    @Override
    public void deleteById(Long id) {
        //条件选择器
        QueryWrapper<SetmealGroup> queryWrapper = new QueryWrapper<>();
        //还可以使用lambda()
        queryWrapper.lambda().eq(SetmealGroup::getSetmealId,id);
        this.remove(queryWrapper);
    }
    @Override
    public void saveSetmealGroup(Long id, Long[] groups) {
        if (groups!=null&&groups.length>0){
            ArrayList<SetmealGroup> list = new ArrayList<>();
            Arrays.stream(groups).forEach((groupId)->{
                SetmealGroup setmealGroup = new SetmealGroup();
                setmealGroup.setSetmealId(id);
                setmealGroup.setGroupId(groupId);
                //这里有疑问
                list.add(setmealGroup);
            });
            this.saveBatch(list);
        }

    }

    @Override
    public Long[] selectItemsByGroupId(Long id) {
        return baseMapper.selectItemsByGroupId(id);
    }
}